import jwt
import datetime

class JWTE:
    def __init__(self):
        self.secrite_key = 'asdfdf&*^*^!@#$%****hasgaj'

    # 加密
    def jwt_en(self,data):
        return str(jwt.encode(data,self.secrite_key,algorithm='HS256'),encoding='utf8')

    # 对比
    def jwt_check(self,token):
        try:
            payload = jwt.decode(token,self.secrite_key,algorithms='HS256')
            newtoken = str(jwt.decode(payload,self.secrite_key,algorithms='HS256'),encoding='utf8')
            if token == newtoken:
                return True
            return True
        except:
            return False

    # 解密
    def jwt_de(self, token):
        try:
            payload = jwt.decode(token, self.secrite_key, algorithms='HS256')
            return payload
        except:
            return None


# import time

myjwt = JWTE()
# data = {'data':{'username':'22','userid':1,'iat':time.time(),'exp':3600}}
# token = jwte.jwt_en(data)
# print(token)

# res = jwte.jwt_de(token)
# print(res)